package login.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import commons.pojo.Ans;
import commons.pojo.Users;
import commons.vo.LivegoodsResult;
import login.dao.AnsMapper;
import login.dao.LoginMapper;
import login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private AnsMapper ansMapper;

    @Override
    public LivegoodsResult setUser(String username, String password) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>().eq("name", username);
        Users user = loginMapper.selectOne(queryWrapper);
//        Users user = loginMapper.selectById(username);  // (new QueryWrapper<Users>().eq("name",username));
//        System.out.println("dsofijosidfnosfojofjoasjflasjofjaslfasnl");
        if (user == null){
            PasswordEncoder encoder = new BCryptPasswordEncoder();
            loginMapper.insert(new Users(username,encoder.encode(password)));
            Users user1 = loginMapper.selectOne(queryWrapper);
            ansMapper.insert(new Ans(user1.getId(),0,0,0,0,0,0,0,0,0,0,0));
            return LivegoodsResult.ok("设置成功");
        }else return LivegoodsResult.error("用户已存在");
    }

    @Override
    public LivegoodsResult loginUser(String username, String password) {
//        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>().eq("name", username);
//        Users user = loginMapper.selectOne(queryWrapper);
////        Users user = loginMapper.selectUser(username);
//        if (user == null){
//            return LivegoodsResult.error("用户不存在");
//        }else{
//            String s = user.getPassword();
//            PasswordEncoder encoder = new BCryptPasswordEncoder();
//            if (encoder.matches(username,s)) {
//                LivegoodsResult ok = LivegoodsResult.ok();
//                ok.setMsg("登录成功");
//                ok.setData(user.getId());
//                return ok;
//            }
//        }
        return LivegoodsResult.error("密码错误");
    }
}
